/****************************************************
          File Name:46-1-msgget.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Thu 11 Sep 2014 03:11:45 PM CST
*****************************************************/

#include"msg.h"
#include<fcntl.h>
#include<string.h>

int main(int argc,char *argv[])
{
    if(strcmp(argv[1],"--help")==0)
    {
        printf("%s -f path\nor%s -p\n",argv[0],argv[0]);
        exit(1);
    }

    int status;
    key_t key;
    int flag,opt;
    int perm=S_IWUSR|S_IRUSR|S_IWGRP;

    while((opt=getopt(argc,argv,"f:p"))!=-1)
    {
        switch(opt)
        {
            case 'f':
                key=ftok(argv[optind],309);
                status=msgget(key,O_CREAT|O_EXCL|perm);
                break;
            case 'p':
                status=msgget(IPC_PRIVATE,O_CREAT|O_EXCL|perm);
                if(status==1 && errno != EEXIST)
                    err("msgget");
                break;
        }
    }
    printf("key: 0x%x\t msgid: %d\n",key,status);
    exit(0);

}
